home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / MPW Related / Animated Cursors / AnimCursor.c next >
Encoding:
C/C++ Source or Header  |  1990-09-14  |  7.7 KB  |  201 lines  |  [TEXT/MPS ]

  1. /***************************************************************************************************\
  2. * Comments About Comments
  3. *
  4. * Variable Declaration/Definition Comments
  5. * ----------------------------------------
  6. *     All variables that declared and defined have a comment right after them.  You'll notice that
  7. * some of those comments are preceded by a -> or a =>.  Read these as “pointer to” and “handle to”
  8. * respectively.
  9. *
  10. * Parameter Declaration Comments
  11. * ------------------------------
  12. *     All declarations for parameters to functions have a comment just after them.  After the
  13. * comment, you'll see one of three signs: <<, <>, or >>.  The sign, <<, means that this parameter is
  14. * ONLY used to pass information back to the calling function.  The sign, >>, means that this
  15. * parameter is ONLY used to pass information to the called function.  The sign, <>, means that this
  16. * parameter is used both to give information to the called function, and to return information to the
  17. * calling function.
  18. *
  19. *     If the parameter is a pointer, these signs refer to the information that is being pointed to.
  20. * If the parameter is a handle, these signs refer to the information that is being, well, “handled”
  21. * to.
  22. \***************************************************************************************************/
  23.  
  24.  
  25. /***************************************************************************************************\
  26. * Header Files
  27. \***************************************************************************************************/
  28.  
  29. #include <Memory.h>
  30. #include <Quickdraw.h>
  31. #include <Resources.h>
  32. #include <ToolUtils.h>
  33. #include "AnimCursor.h"
  34.  
  35.  
  36. /***************************************************************************************************\
  37. * Constant Declarations
  38. \***************************************************************************************************/
  39.  
  40. #define null 0 /* Generic Null */
  41.  
  42.  
  43. /***************************************************************************************************\
  44. * GetAnimCurs - Retrieve an animated cursor list and its cursors
  45. *
  46. * Coding Notes:
  47. * #A# - We need to make the AnimCurs non-purgeable for it’s life span.
  48. * #B# - If the high bit of the frame field is set, AnimCurs is a list of color cursors.
  49. * #C# - The cursor list is a list of CursHandles, but in the resource file the cursor list is a list
  50. *       of resource IDs in the upper 16 bits of the CursHandles.  That’s why there’s all this weird
  51. *       casting about in the GetCursor and GetCCursor calls.
  52. * #D# - Replace the resource IDs with CursHandles.
  53. * #E# - If the cursor is a non-color cursor ('CURS' resource), make it non-purgeable.  GetCColor
  54. *       returns copies of the 'crsr' resources so they are already non-purgeable.
  55. \***************************************************************************************************/
  56.  
  57. pascal AnimCursHnd
  58. GetAnimCurs (AnimCursID)
  59.     short AnimCursID; /* Resource ID of animated cursor list ('acur' resource) >> */
  60.     {
  61.     AnimCursHnd AnimCurs;  /* => Animated cursor list */
  62.     CursHandle  TheCursor; /* => One cursor in AnimCurs */
  63.     short       ColorCurs; /* True if cursor list has color cursors, false otherwise */
  64.     short       Index;     /* Index into animated cursor list */
  65.  
  66.     if ((AnimCurs = (AnimCursHnd) GetResource ('acur', AnimCursID)) != (AnimCursHnd) null)
  67.         {
  68.         HNoPurge ((Handle) AnimCurs); /* #A# */
  69.         ColorCurs = (**AnimCurs).frame & 0x8000; /* #B# */
  70.         for (Index = 0; Index < (**AnimCurs).count; Index++)
  71.             {
  72.             if (ColorCurs) /* #C# */
  73.                 TheCursor = (CursHandle) GetCCursor (*((short *) &((**AnimCurs).cursors [Index])));
  74.             else
  75.                 TheCursor = GetCursor (*((short *) &((**AnimCurs).cursors [Index])));
  76.             (**AnimCurs).cursors [Index] = TheCursor; /* #D# */
  77.             if ((!ColorCurs) && (TheCursor != (CursHandle) null)) /* #E# */
  78.                 HNoPurge ((Handle) TheCursor);
  79.             }
  80.         }
  81.     return AnimCurs;
  82.     }
  83.  
  84.  
  85. /***************************************************************************************************\
  86. * AnimateCurs - Animate a cursor list by one frame
  87. *
  88. * Coding Notes:
  89. * #A# - If the high bit of the frame field is set, AnimCurs is a list of color cursors.
  90. \***************************************************************************************************/
  91.  
  92. pascal void
  93. AnimateCurs (AnimCurs)
  94.     AnimCursHnd AnimCurs; /* => Cursor list to animate <> */
  95.     {
  96.     CursHandle TheCursor; /* => The cursor to display */
  97.     short      ColorCurs; /* True if AnimCurs is a list of color cursors */
  98.     short      FrameNum;  /* Index in the cursor list of the cursor to display */
  99.  
  100.     ColorCurs = (**AnimCurs).frame & 0x8000; /* #A# */
  101.     FrameNum = (**AnimCurs).frame & 0x7FFF;
  102.     if ((TheCursor = (**AnimCurs).cursors [FrameNum]) != (CursHandle) null)
  103.         if (ColorCurs)
  104.             SetCCursor ((CCrsrHandle) TheCursor);
  105.         else
  106.             {
  107.             HLock ((Handle) TheCursor);
  108.             SetCursor (*TheCursor);
  109.             HUnlock ((Handle) TheCursor);
  110.             }
  111.     if (ColorCurs)
  112.         (**AnimCurs).frame = ((((**AnimCurs).frame & 0x7FFF) + 1) % (**AnimCurs).count) | 0x8000; /* #A# */
  113.     else
  114.         (**AnimCurs).frame = ((**AnimCurs).frame + 1) % (**AnimCurs).count;
  115.     }
  116.  
  117.  
  118. /***************************************************************************************************\
  119. * AnimateProgCurs - Animate a cursor list one frame to show progress
  120. *
  121. * Coding Notes:
  122. * #A# - If the high bit of the frame field is set, AnimCurs is a list of color cursors.
  123. * #B# - The calculations do this mapping:
  124. *
  125. *             Val
  126. *              |
  127. * Min----------------------------------------------Max
  128. *
  129. * Which maps to:
  130. *
  131. *           ValLevel
  132. *              |
  133. * 0.0----------------------------------------------1.0
  134. *
  135. * Which maps to:
  136. *
  137. *           CursLevel
  138. *              |
  139. * 0.0----------------------------------------------(**AnimCurs).count in Fixed format
  140. *
  141. * Which maps to:
  142. *
  143. *           CursInd
  144. *              |
  145. *  0-----------------------------------------------(**AnimCurs).count
  146. *
  147. \***************************************************************************************************/
  148.  
  149. pascal void
  150. AnimateProgCurs (AnimCurs, Min, Max, Val)
  151.     AnimCursHnd AnimCurs; /* => Animated cursor list to animate >> */
  152.     short       Min;      /* Minimum value of Val >> */
  153.     short       Max;      /* Maximum value of Val >> */
  154.     short       Val;      /* Value to set cursor by >> */
  155.     {
  156.     Fixed      ValLevel;  /* If Val=Min then ValLevel=0.0, If Val=Max then ValLevel=1.0 */
  157.     Fixed      CursLevel; /* If ValLevel=0.0 then CursLevel=0.0, If ValLevel=1.0 then CursLevel=count */
  158.     short      CursInd;   /* Truncated value of CursLevel */
  159.     CursHandle TheCursor; /* => Cursor to display */
  160.  
  161.     Max -= Min; /* #B# */
  162.     Val -= Min;
  163.     ValLevel = FixRatio (Val, Max);
  164.     CursLevel = ((**AnimCurs).count - 1) << 16;
  165.     CursLevel = FixMul (CursLevel, ValLevel);
  166.     CursInd = CursLevel >> 16;
  167.     if ((TheCursor = (**AnimCurs).cursors [CursInd]) != (CursHandle) null)
  168.         if ((**AnimCurs).frame & 0x8000) /* #A# */
  169.             SetCCursor ((CCrsrHandle) TheCursor);
  170.         else
  171.             {
  172.             HLock ((Handle) TheCursor);
  173.             SetCursor (*TheCursor);
  174.             HUnlock ((Handle) TheCursor);
  175.             }
  176.     }
  177.  
  178.  
  179. /***************************************************************************************************\
  180. * DisposeAnimCurs - Dispose of a cursor list and its cursors
  181. *
  182. * Coding Notes:
  183. * #A# - If the high bit of the frame field is set, AnimCurs is a list of color cursors.
  184. * #B# - CURS resources aren’t actually disposed of, they’re just made purgeable.
  185. \***************************************************************************************************/
  186.  
  187. pascal void
  188. DisposeAnimCurs (AnimCurs)
  189.     AnimCursHnd AnimCurs;
  190.     {
  191.     short Index;
  192.  
  193.     for (Index = 0; Index < (**AnimCurs).count; Index++)
  194.         if ((**AnimCurs).cursors != (CursHandle) null)
  195.             if ((**AnimCurs).frame & 0x8000) /* #A# */
  196.                 DisposCCursor ((CCrsrHandle) (**AnimCurs).cursors [Index]);
  197.             else
  198.                 HPurge ((Handle) (**AnimCurs).cursors [Index]); /* #B# */
  199.     ReleaseResource ((Handle) AnimCurs);
  200.     }
  201.